package com.mmg.exception;


import com.mmg.common.Result;
import com.mmg.common.ResultCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;


@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(ArithmeticException.class)
    public Result arithmeticException(ArithmeticException e) {
        log.info("算数异常:" + e.getMessage());
        e.printStackTrace();
        return Result.error(ResultCode.ARITHMETIC_EXCEPTION);
    }

    @ExceptionHandler(NullPointerException.class)
    public Result nullPointerException(NullPointerException e) {
        e.printStackTrace();
        return Result.error(ResultCode.NULL_POINTER_EXCEPTION);
    }

    @ExceptionHandler(ArrayIndexOutOfBoundsException.class)
    public Result arrayIndexOutOfBoundsException(ArrayIndexOutOfBoundsException e) {
        e.printStackTrace();
        return Result.error(ResultCode.ARRAY_INDEX_OUT_OF_BOUNDS_EXCEPTION);
    }

    @ExceptionHandler(Exception.class)
    public Result arrayIndexOutOfBoundsException(Exception e) {
        e.printStackTrace();
        return Result.error(ResultCode.UNKNOWN_EXCEPTION);
    }

    @ExceptionHandler(BusinessException.class)
    public Result arrayIndexOutOfBoundsException(BusinessException e) {
        e.printStackTrace();
        return Result.error()
                .code(e.getCode())
                .message(e.getErrMsg());
    }
}
